package com.tqw.order.controller;

import com.tqw.constant.SecurityConstants;
import com.tqw.entity.JWTUtil;
import com.tqw.entity.Result;
import com.tqw.order.service.OrderService;
import com.tqw.order.vo.OrderDescVO;
import com.tqw.order.vo.PageOrderResult;
import com.tqw.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: czm
 * @Date: 2022/04/17/15:06
 * @Description:
 */
@RestController
@RequestMapping("/order/myOrder")
public class MyOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取订单列表
     * @param pageResult
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestHeader("Authorization") String token, PageOrderResult pageResult){
        Integer clientId = JWTUtil.getId(token);
        pageResult.setClientId(clientId);
        PageOrderResult orderList = orderService.getOrderList(pageResult);
        Result<Object> result = new Result<>();
        result.setData(orderList);
        return result;
    }

    /**
     * 根据ID获取订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/orderDesc/{orderId}")
    public Result<OrderDescVO> getOrderDesc(@RequestHeader("Authorization") String token,@PathVariable String orderId){
        Integer clientId = JWTUtil.getId(token);
        OrderDescVO orderDesc = orderService.getOrderDesc(orderId,clientId);
        Result<OrderDescVO> result = new Result<>();
        result.setData(orderDesc);
        return result;
    }

    /**
     * 根据订单ID删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("/delete/{orderId}")
    public Result delete(@PathVariable String orderId){
        orderService.delete(orderId);
        return new Result();
    }

    /**
     * 根据订单ID取消订单
     * @param orderId
     * @return
     */
    @PutMapping("/cancel/{orderId}")
    public Result cancel(@PathVariable String orderId){
        orderService.cancel(orderId);
        return new Result();
    }

}
